#include<QtGui>
#include "mylist.h"
Mylist::Mylist(const int h, const int w){
	setFixedSize(w,h);
	list = new QDir();
	cur = 0;
  	filters << "*.txt";
  	l = list->entryList(filters);
  	for (int i = 0; i < l.size(); i++)
  		l[i] = l[i].remove(l[i].size()-4,4);
	update();
}
void Mylist::next(){cur++; if (cur >= l.size()) cur = 0; emit currentIndexChanged(l[cur]); update();}
void Mylist::prev(){cur--; if (cur < 0) cur = l.size()-1; emit currentIndexChanged(l[cur]); update();}
const QString & Mylist::current(){return l[cur];}
void Mylist::reloadList(){
	list = new QDir();
	cur = 0;
	filters << "*.txt";
	l = list->entryList(filters);
  	for (int i = 0; i < l.size(); i++)
  		l[i] = l[i].remove(l[i].size()-4,4);
	emit currentIndexChanged(l[cur]); 
	update();
}
void Mylist::paintEvent ( QPaintEvent * event ){
	QPainter painter(this);
	painter.setBrush(Qt::white);
	painter.setPen(Qt::gray);
	painter.drawRect(0,0,width()-1,height()-1);
	int shift = 0;
	if ((cur+1)*fontMetrics().height() > height())
		shift = (cur-height()/fontMetrics().height()+1);
	QColor even = QColor(105, 184, 193, 150);
	QColor odd = QColor(160, 222, 229, 150);
	QColor cc = QColor(112, 115, 115, 150);
	for (int i = shift; i < l.size(); i++){
		painter.save();
		
		if (i == cur){
			painter.setFont(QFont("Arial",5));
			painter.setBrush(cc);
		}else { painter.setFont(QFont("Arial",4));
			if (i % 2 == 0) painter.setBrush(even);
			else painter.setBrush(odd);
		}
		painter.translate(1, fontMetrics().height()*(i-shift)+fontMetrics().height());
		painter.drawRect(0,-fontMetrics().height()+1,width()-1,fontMetrics().height());
		painter.setPen(Qt::black);
		painter.drawText(0,0, QString("%1. %2").arg(i+1).arg(l[i]));
		painter.restore();
	}
}
